using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class HL_Admin_Admin_LabelSqlBuild : Admin, IRequiresSessionState
{
	private SqlConnection SqlConn;
	private OleDbConnection OleConn;
	private SqlDataAdapter Sql_da;
	private string m_LabelDataType = "";
	private string m_LabelConnstr = "";
	private string querryStr = "";
	private LabelBll _lb = null;
	protected TextBox OutPutNums;
	protected DropDownList MainSheet;
	protected DropDownList SecondSheet;
	protected ListBox MainList;
	protected ListBox SecondList;
	protected DropDownList JoinList;
	protected DropDownList MainList_Join;
	protected DropDownList JoinLink;
	protected DropDownList SecondList_Join;
	protected HtmlTableRow linkCondition;
	protected Button CreateSQLSearchButton;
	protected PlaceHolder CreateSQLSearch;
	protected Literal parmlist;
	protected Button BuildSQl_Button;
	protected TextBox SqlContent;
	protected CheckBox UsePage;
	protected TextBox CountPage;
	protected Button PreStepButton;
	protected Button NextStepButton;
	protected Button SaveButton;
	protected HiddenField H_flag;
	protected LabelBll lb
	{
		get
		{
			LabelBll lb;
			if (this._lb == null)
			{
				this._lb = new LabelBll(this.conn);
				lb = this._lb;
			}
			else
			{
				lb = this._lb;
			}
			return lb;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		this.BindPage();
		if (this.H_flag.Value == "true")
		{
			this.CreateSQLSearchTable();
		}
	}
	protected void BindPage()
	{
		if (!string.IsNullOrEmpty(Fetch.Get("name")))
		{
			DataTable dataTable = new DataTable();
			DataTable dataTable2 = new DataTable();
			if (Fetch.Get("action") == "add" && this.Session[Fetch.Get("name")] != null)
			{
				dataTable2 = (DataTable)this.Session[Fetch.Get("name")];
				if (this.Session["Parm_Table_" + Fetch.Get("name")] != null)
				{
					dataTable = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
				}
			}
			else
			{
				if (Fetch.Get("action") == "modify" && Fetch.GetID("ID") > 0)
				{
					int iD = Fetch.GetID("ID");
					DataTable labelByLabelID = this.lb.GetLabelByLabelID(iD);
					if (labelByLabelID.Rows.Count == 1)
					{
						dataTable = this.lb.GetParmDataTable(labelByLabelID.Rows[0]["Define"].ToString());
					}
					dataTable2 = this.lb.GetLabelDefineByLabelID(iD);
					this.SaveButton.Visible = true;
				}
			}
			this.m_LabelDataType = dataTable2.Rows[0]["LabelDataType"].ToString();
			this.m_LabelConnstr = dataTable2.Rows[0]["ConnStr"].ToString();
			if (!base.IsPostBack)
			{
				this.SqlContent.Text = dataTable2.Rows[0]["SqlStr"].ToString();
				this.CountPage.Text = dataTable2.Rows[0]["PageSqlStr"].ToString();
				if (dataTable2.Rows[0]["EnablePage"].ToString() == "1")
				{
					this.UsePage.Checked = true;
				}
			}
			string labelDataType = this.m_LabelDataType;
			if (labelDataType != null)
			{
				if (!(labelDataType == "sql_sysquery"))
				{
					if (!(labelDataType == "sql_outquery"))
					{
						if (!(labelDataType == "sql_sysstorequery"))
						{
							if (!(labelDataType == "mdb"))
							{
								if (!(labelDataType == "xml"))
								{
									if (labelDataType == "mysql")
									{
										this.m_LabelDataType = "MySql";
									}
								}
							}
							else
							{
								this.m_LabelConnstr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + base.Server.MapPath("..") + "\\" + this.m_LabelConnstr;
								this.m_LabelDataType = "Access";
								this.querryStr = "SELECT * FROM MSysObjects WHERE Flags=0 AND Type=1";
							}
						}
						else
						{
							this.m_LabelDataType = "SQL_Store";
							this.SqlContent.Text = this.m_LabelConnstr;
						}
					}
					else
					{
						this.m_LabelDataType = "SQL";
						this.querryStr = "SELECT name FROM sysobjects where xtype='U' ORDER BY name";
					}
				}
				else
				{
					this.m_LabelConnstr = ConfigurationManager.ConnectionStrings["HowaveConnectionString"].ConnectionString;
					this.m_LabelDataType = "SQL";
					this.querryStr = "SELECT name FROM sysobjects where xtype='U' ORDER BY name";
				}
			}
			this.UsePage.Attributes.Add("onclick", "showHideCountPage()");
			this.SqlContent.Attributes.Add("onmouseup", "dragend(1);");
			this.SqlContent.Attributes.Add("onmousemove", "movePoint();");
			if (!base.IsPostBack)
			{
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					foreach (DataRow dataRow in dataTable.Rows)
					{
						Literal expr_452 = this.parmlist;
						expr_452.Text = expr_452.Text + "<div onmousedown=\"dragstart();\" class=\"label_square\">" + dataRow["name"].ToString() + "</div>";
					}
				}
				else
				{
					this.parmlist.Text = string.Concat(new string[]
					{
						"你还没有添加任何参数<a href=\"Admin_LabelParms.aspx?action=",
						base.Request["action"],
						"id=",
						base.Request["id"],
						"&name=",
						base.Request["name"],
						"\" class=\"label_square\">添加标签</a>"
					});
				}
				this.BindTableName(this.m_LabelDataType, this.m_LabelConnstr, this.querryStr);
			}
		}
	}
	public DataTable GetDataTable(string DbType, string ConnStr, string queryString)
	{
		DataSet dataSet = new DataSet();
		DataTable dataTable = new DataTable();
		DataTable result;
		try
		{
			if (DbType != null)
			{
				if (!(DbType == "SQL"))
				{
					if (!(DbType == "Access"))
					{
						if (!(DbType == "MySql"))
						{
						}
					}
					else
					{
						object[] restrictions = new object[]
						{
							null,
							null,
							null,
							"table"
						};
						this.OleConn = new OleDbConnection(ConnStr);
						this.OleConn.Open();
						DataTable oleDbSchemaTable = this.OleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, restrictions);
						dataTable.Columns.Add("table_name");
						foreach (DataRow dataRow in oleDbSchemaTable.Rows)
						{
							DataRow dataRow2 = dataTable.NewRow();
							dataRow2[0] = dataRow["TABLE_NAME"].ToString();
							dataTable.Rows.Add(dataRow2);
						}
						this.OleConn.Close();
					}
				}
				else
				{
					this.SqlConn = new SqlConnection(ConnStr);
					this.Sql_da = new SqlDataAdapter();
					this.Sql_da.SelectCommand = new SqlCommand(queryString, this.SqlConn);
					this.Sql_da.Fill(dataSet, "MyTable");
					dataTable = dataSet.Tables["MyTable"];
				}
			}
			result = dataTable;
		}
		catch
		{
			result = null;
		}
		return result;
	}
	protected void BindTableName(string DbType, string ConnStr, string querryStr)
	{
		this.MainSheet.Items.Clear();
		this.SecondSheet.Items.Clear();
		this.MainSheet.Items.Add(new ListItem("请选择一个表", "-1"));
		this.SecondSheet.Items.Add(new ListItem("请选择一个表", "-1"));
		DataTable dataTable = this.GetDataTable(DbType, ConnStr, querryStr);
		if (dataTable != null)
		{
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				ListItem item = new ListItem(dataTable.Rows[i][0].ToString(), dataTable.Rows[i][0].ToString());
				this.MainSheet.Items.Add(item);
				this.SecondSheet.Items.Add(item);
			}
		}
	}
	public void BindListTable(string DbType, string ConnStr, string TableName, string ControlID)
	{
		string queryString = "select top 1 * from [" + TableName + "] where 1=1";
		if (DbType == "MySql")
		{
			queryString = "select top 1 * from " + TableName + " where 1";
		}
		ListBox listBox = (ListBox)this.Page.Master.FindControl("ContentPlaceHolder1").FindControl(ControlID);
		DropDownList dropDownList = (DropDownList)this.Page.Master.FindControl("ContentPlaceHolder1").FindControl(ControlID + "_Join");
		listBox.Items.Clear();
		dropDownList.Items.Clear();
		DataTable dataTable = this.GetDataTable(DbType, ConnStr, queryString);
		foreach (DataColumn dataColumn in dataTable.Columns)
		{
			ListItem item = new ListItem(dataColumn.ColumnName, dataColumn.DataType.ToString());
			listBox.Items.Add(item);
			dropDownList.Items.Add(item);
		}
	}
	protected void MainSheet_SelectIndexChanged(object sender, EventArgs e)
	{
		if (this.MainSheet.SelectedValue != "" && this.MainSheet.SelectedValue != "-1")
		{
			this.BindListTable(this.m_LabelDataType, this.m_LabelConnstr, this.MainSheet.SelectedValue, "MainList");
		}
	}
	protected void SecondSheet_SelectIndexChanged(object sender, EventArgs e)
	{
		if (this.SecondSheet.SelectedValue != "" && this.SecondSheet.SelectedValue != "-1")
		{
			this.linkCondition.Visible = true;
			this.BindListTable(this.m_LabelDataType, this.m_LabelConnstr, this.SecondSheet.SelectedValue, "SecondList");
		}
		else
		{
			this.linkCondition.Visible = false;
			this.SecondList.Items.Clear();
		}
	}
	protected void BuildSQl_Button_Click(object sender, EventArgs e)
	{
		string text = this.OutPutNums.Text;
		string selectedValue = this.MainSheet.SelectedValue;
		string selectedValue2 = this.SecondSheet.SelectedValue;
		string text2 = "";
		string text3 = "";
		string text4 = "select top " + text + " ";
		bool flag = false;
		if (this.MainList.SelectedValue != "" && this.MainList.SelectedValue != "-1")
		{
			foreach (ListItem listItem in this.MainList.Items)
			{
				if (listItem.Selected)
				{
					string text5 = text2;
					text2 = string.Concat(new string[]
					{
						text5,
						selectedValue,
						".",
						listItem.Text,
						","
					});
					flag = true;
				}
			}
			if (!flag)
			{
				text2 = selectedValue + ".*,";
			}
			if (this.SecondList.SelectedValue != "-1" && this.SecondList.SelectedValue != "")
			{
				foreach (ListItem listItem in this.SecondList.Items)
				{
					if (listItem.Selected)
					{
						string text5 = text2;
						text2 = string.Concat(new string[]
						{
							text5,
							selectedValue2,
							".",
							listItem.Text,
							","
						});
					}
				}
				text3 = string.Concat(new string[]
				{
					" ",
					this.JoinList.SelectedValue,
					" ",
					selectedValue2,
					" on ",
					selectedValue,
					".",
					this.MainList_Join.SelectedItem.Text,
					" ",
					this.JoinLink.SelectedValue,
					selectedValue2,
					".",
					this.SecondList_Join.SelectedItem.Text,
					" "
				});
			}
			if (!flag)
			{
				text2 = text2 + selectedValue2 + ".*,";
			}
			text4 += text2.Remove(text2.Length - 1);
			text4 = text4 + " from " + selectedValue + text3;
			Table table = (Table)this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("condition_table");
			if (table != null)
			{
				string text6 = "";
				string text7 = "";
				for (int i = 1; i < table.Rows.Count; i++)
				{
					TableRow tableRow = table.Rows[i];
					TextBox textBox = (TextBox)tableRow.Cells[3].Controls[0];
					DropDownList dropDownList = (DropDownList)tableRow.Cells[2].Controls[0];
					DropDownList dropDownList2 = (DropDownList)tableRow.Cells[4].Controls[0];
					string text8 = tableRow.Cells[1].Text;
					if (textBox.Text != null && textBox.Text != "")
					{
						string text5 = text6;
						text6 = string.Concat(new string[]
						{
							text5,
							" (",
							selectedValue,
							".",
							tableRow.Cells[0].Text
						});
						if (text8 == "System.Int32" || text8 == "System.Int16")
						{
							text6 = text6 + dropDownList.SelectedValue + textBox.Text + ") and";
						}
						else
						{
							text5 = text6;
							text6 = string.Concat(new string[]
							{
								text5,
								dropDownList.SelectedValue,
								"'",
								textBox.Text,
								"') and"
							});
						}
					}
					if (dropDownList2.SelectedValue != "0")
					{
						string text5 = text7;
						text7 = string.Concat(new string[]
						{
							text5,
							selectedValue,
							".",
							tableRow.Cells[0].Text,
							dropDownList2.SelectedValue,
							","
						});
					}
				}
				if (text6 != "")
				{
					text4 = text4 + " where " + text6.Remove(text6.Length - 3);
				}
				if (text7 != "")
				{
					text4 = text4 + " Order By " + text7.Remove(text7.Length - 1);
				}
			}
			this.SqlContent.Text = text4;
			this.CountPage.Text = "SELECT count(*)  FROM " + selectedValue + text3;
		}
	}
	protected void CreateSQLSearchButton_Click(object sender, EventArgs e)
	{
		this.CreateSQLSearchTable();
	}
	protected void CreateSQLSearchTable()
	{
		if (this.MainList.SelectedValue != "" && this.MainList.SelectedValue != "-1")
		{
			this.CreateSQLSearch.Controls.Clear();
			this.H_flag.Value = "true";
			Table table = new Table();
			table.ID = "condition_table";
			TableHeaderRow tableHeaderRow = new TableHeaderRow();
			TableHeaderCell tableHeaderCell = new TableHeaderCell();
			tableHeaderCell.Text = "字段名";
			TableHeaderCell tableHeaderCell2 = new TableHeaderCell();
			tableHeaderCell2.Text = "字段类型";
			TableHeaderCell tableHeaderCell3 = new TableHeaderCell();
			tableHeaderCell3.Text = "条件约束";
			TableHeaderCell tableHeaderCell4 = new TableHeaderCell();
			tableHeaderCell4.Text = "条件值";
			TableHeaderCell tableHeaderCell5 = new TableHeaderCell();
			tableHeaderCell5.Text = "排序";
			tableHeaderRow.Cells.Add(tableHeaderCell);
			tableHeaderRow.Cells.Add(tableHeaderCell2);
			tableHeaderRow.Cells.Add(tableHeaderCell3);
			tableHeaderRow.Cells.Add(tableHeaderCell4);
			tableHeaderRow.Cells.Add(tableHeaderCell5);
			table.Rows.Add(tableHeaderRow);
			for (int i = 0; i < this.MainList.Items.Count; i++)
			{
				if (this.MainList.Items[i].Selected)
				{
					TableRow tableRow = new TableRow();
					TableCell tableCell = new TableCell();
					TableCell tableCell2 = new TableCell();
					TableCell tableCell3 = new TableCell();
					TableCell tableCell4 = new TableCell();
					TableCell tableCell5 = new TableCell();
					TextBox textBox = new TextBox();
					tableCell.Text = this.MainList.Items[i].Text;
					tableRow.Cells.Add(tableCell);
					tableCell2.Text = this.MainList.Items[i].Value;
					tableRow.Cells.Add(tableCell2);
					DropDownList dropDownList = new DropDownList();
					dropDownList.Items.Add(new ListItem("等于", " = "));
					dropDownList.Items.Add(new ListItem("大于", " > "));
					dropDownList.Items.Add(new ListItem("小于", " < "));
					dropDownList.Items.Add(new ListItem("大于等于", " >= "));
					dropDownList.Items.Add(new ListItem("小于等于", " <= "));
					dropDownList.Items.Add(new ListItem("不等于", " <> "));
					dropDownList.Items.Add(new ListItem("像", " like "));
					dropDownList.Items.Add(new ListItem("不像", " Not like "));
					dropDownList.Items.Add(new ListItem("在", " in "));
					dropDownList.Items.Add(new ListItem("不在", " Not in "));
					dropDownList.ID = "ddl_condition_" + i.ToString();
					tableCell3.Controls.Add(dropDownList);
					tableRow.Cells.Add(tableCell3);
					textBox.ID = "tb_value" + i.ToString();
					tableCell4.Controls.Add(textBox);
					tableRow.Cells.Add(tableCell4);
					DropDownList dropDownList2 = new DropDownList();
					dropDownList2.Items.Add(new ListItem("不排序", "0"));
					dropDownList2.Items.Add(new ListItem("升序", " asc "));
					dropDownList2.Items.Add(new ListItem("降序", " desc "));
					dropDownList2.ID = "ddl_sort_" + i.ToString();
					tableCell5.Controls.Add(dropDownList2);
					tableRow.Cells.Add(tableCell5);
					table.Rows.Add(tableRow);
				}
			}
			this.CreateSQLSearch.Controls.Add(table);
		}
	}
	protected void PreStepButton_Click(object sender, EventArgs e)
	{
		if (base.Request["action"] != null && base.Request["name"] != null)
		{
			if (base.Request["action"] == "add")
			{
				base.Response.Redirect("Admin_LabelParms.aspx?action=add&id=0&name=" + base.Request["name"]);
			}
			else
			{
				base.Response.Redirect("Admin_LabelParms.aspx?action=modify&id=" + base.Request["id"] + "&name=" + base.Request["name"]);
			}
		}
	}
	protected void NextStepButton_Click(object sender, EventArgs e)
	{
		if (base.Request["action"] != null && base.Request["name"] != null)
		{
			if (base.Request["action"] == "add" && this.Session[base.Request["name"]] != null)
			{
				DataTable dataTable = (DataTable)this.Session[base.Request["name"]];
				if (dataTable.Rows.Count > 0)
				{
					dataTable.Rows[0]["SqlStr"] = this.SqlContent.Text;
					if (this.UsePage.Checked)
					{
						dataTable.Rows[0]["EnablePage"] = "1";
						dataTable.Rows[0]["PageSqlStr"] = this.CountPage.Text;
					}
					else
					{
						dataTable.Rows[0]["EnablePage"] = "0";
					}
				}
				base.Response.Redirect("Admin_LabelTemplate.aspx?action=add&id=0&name=" + base.Request["name"]);
			}
			else
			{
				base.Response.Redirect("Admin_LabelTemplate.aspx?action=modify&id=" + base.Request["id"] + "&name=" + base.Request["name"]);
			}
		}
	}
	protected void SaveButton_Click(object sender, EventArgs e)
	{
		int iD = Fetch.GetID("ID");
		string enableLabelPageCount = "0";
		if (this.UsePage.Checked)
		{
			enableLabelPageCount = "1";
		}
		string[,] parmArray = new string[0, 0];
		if (this.lb.UpdateLabel(iD, "", "", "", "", "", "", "", "", this.SqlContent.Text, enableLabelPageCount, this.CountPage.Text, "", "", parmArray))
		{
			this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_Lable.aspx");
		}
	}
}
